import { reactive, ref, toRefs, getCurrentInstance } from "vue"

export default function () {
    let dataParams = reactive({
        arr_data: [],
        loading: true,
        search_btn: false,
        resultObj: null,
    })
    const { proxy } = getCurrentInstance()

    const totalNums = ref(0)
    const page = ref(1)
    const pageSize = ref(20) // 每页显示几条
    const pageSizeNum = ref([20, 30, 40, 50]) //调整每页显示条数
    const search_text = ref("")

    // 正常分页
    function pubParams(getList) {
        dataParams.loading = true
        dataParams.arr_data = []
        getList()
    }

    // 刷新使用
    function pubRefreshParams(getList) {
        totalNums.value = 0
        page.value = 1
        dataParams.loading = true
        dataParams.arr_data = []
        getList()
    }

    // 列表
    function getdata(url, datas) {
        proxy.$axios
            .get(url, { params: datas })
            .then((res) => {
                dataParams.loading = false
                dataParams.search_btn = false
                good_succ(url, res)
            })
            .catch((error) => {
                dataParams.loading = false
            })
    }

    function good_succ(url, res) {
        switch (res.data.code) {
            case 0:
                let result = res.data.data
                dataParams.resultObj = res.data
                // 订单列表
                if (url === 'order/index') {
                    result.map(item => {
                        let order_product = item.order.order_product
                        item.goodMoreSwitch = order_product.length > 8 ? true : false
                    })
                }
                totalNums.value = res.data.count
                dataParams.arr_data = result
                break
            default:
                proxy.$message({ message: res.data.msg, type: "error" })
                break
        }
    }

    return { ...toRefs(dataParams), page, pageSize, pageSizeNum, totalNums, search_text, pubParams, pubRefreshParams, getdata }
}
